﻿package com.thorqq.magictimer.timeLoop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.thorqq.magictimer.AdapterChooseMonthWeekDay;
import com.thorqq.magictimer.R;
import com.thorqq.magictimer.core.ChildView;
import com.thorqq.magictimer.core.ChildViewParent;
import com.thorqq.magictimer.core.TLoopPolicy;
import com.thorqq.magictimer.util.Util;
import com.thorqq.magictimer.widget.GridViewWithDivider;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class ChildViewLoopMonDay extends ChildView
{
    @SuppressWarnings("unused")
    private ChildViewParent mParent;
    private TLoopPolicyMon mPolicy;
    private Context mContext;
    private LayoutInflater mInflater;

    private ArrayList<Map<String, Object>> mMonthList = new ArrayList<Map<String, Object>>();
    private AdapterChooseMonthWeekDay mMonthAdapter;
    private GridViewWithDivider mMonthGridView;

    private ArrayList<Map<String, Object>> mDayList = new ArrayList<Map<String, Object>>();
    private AdapterChooseMonthWeekDay mDayAdapter;
    private GridViewWithDivider mDayGridView;
    
    private CheckBox mCBLunarFlag;

    private TextView mTvMonthDayAll;
    private TextView mTvMonthDayNone;
    private TextView mTvMonthDayReverse;

    public ChildViewLoopMonDay(Context context, TLoopPolicy policy)
    {
        mContext = context;
        mPolicy = (TLoopPolicyMon) policy;
    }

    @Override
    public void updateLayout()
    {
        mCBLunarFlag.setChecked(mPolicy.getLunarFlag() == 1 ? true : false);
    }

    @Override
    protected void registerListener()
    {
        mCBLunarFlag.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked)
            {
                mPolicy.setLunarFlag(isChecked == true ? 1 : 0);
            }
        });

        // 月内日
        //全选
        mTvMonthDayAll.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                char[] mask = mPolicy.getDayMask();
                for(int i = 0; i < mask.length; i++)
                {
                    mask[i] = '1';
                }
                mDayAdapter.notifyDataSetChanged();
            }
        });
        
        //全不选
        mTvMonthDayNone.setOnClickListener(new View.OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                char[] mask = mPolicy.getDayMask();
                for(int i = 0; i < mask.length; i++)
                {
                    mask[i] = '0';
                }
                mDayAdapter.notifyDataSetChanged();
            }
        });
        
        //反选
        mTvMonthDayReverse.setOnClickListener(new View.OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                char[] mask = mPolicy.getDayMask();
                for(int i = 0; i < mask.length; i++)
                {
                    mask[i] = (mask[i] == '1' ? '0' : '1');
                }
                mDayAdapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    protected void doInitLayout(ChildViewParent parent)
    {
        Util.log("init ChildViewLoopMonDay");
        mInflater = LayoutInflater.from(mContext);
        mView = mInflater.inflate(R.layout.loop_item_child_mon_day, null);
        mParent = parent;
        
        mCBLunarFlag= (CheckBox) mView.findViewById(R.id.checkBoxLunarFlag);

        mTvMonthDayAll = (TextView) mView.findViewById(R.id.textViewMonthDayAll);        
        mTvMonthDayNone = (TextView) mView.findViewById(R.id.textViewMonthDayNone);        
        mTvMonthDayReverse = (TextView) mView.findViewById(R.id.textViewMonthDayReverse);

        // 月
        mMonthGridView = ((GridViewWithDivider) mView.findViewById(R.id.GridViewWithDividerMonth));
        mMonthAdapter = new AdapterChooseMonthWeekDay(mContext, mMonthList);
        mMonthGridView.setAdapter(mMonthAdapter);
        mMonthGridView.setHorizontalDivider(mView.getResources().getDrawable(R.drawable.gridview_divider_hor));
        mMonthGridView.setHorizontalDividerHeight(1);
        mMonthGridView.setVerticalDivider(mView.getResources().getDrawable(R.drawable.gridview_divider_ver));
        mMonthGridView.setVerticalDividerWidth(1);
        mMonthGridView.setColumnNum(6);
        
        mMonthList.clear();
        String[] monthText = {"1","2","3","4","5","6","7","8","9","10","11","12"};
        for(int i = 0; i < monthText.length; i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("text", monthText[i]);
            map.put("mask", mPolicy.getMonthMask());
            map.put("listener", new MonthGridListener(mContext, i));
            mMonthList.add(map);            
        }

        mMonthAdapter.notifyDataSetChanged();   
        
        //日
        mDayGridView = ((GridViewWithDivider) mView.findViewById(R.id.GridViewWithDividerDay));
        mDayAdapter = new AdapterChooseMonthWeekDay(mContext, mDayList);
        mDayGridView.setAdapter(mDayAdapter);
        mDayGridView.setHorizontalDivider(mView.getResources().getDrawable(R.drawable.gridview_divider_hor));
        mDayGridView.setHorizontalDividerHeight(1);
        mDayGridView.setVerticalDivider(mView.getResources().getDrawable(R.drawable.gridview_divider_ver));
        mDayGridView.setVerticalDividerWidth(1);
        mDayGridView.setColumnNum(6);
        
        mDayList.clear();
        String[] dayText = {"1","2","3","4","5","6","7","8","9","10",
                "11","12","13","14","15","16","17","18","19","20",
                "21","22","23","24","25","26","27","28","29","30",
                "31","-1"};
        for(int i = 0; i < dayText.length; i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("text", dayText[i]);
            map.put("mask", mPolicy.getDayMask());
            map.put("listener", new MonthDayGridListener(mContext, i));
            mDayList.add(map);            
        }

        mDayAdapter.notifyDataSetChanged();   

    }

    @Override
    protected void doSetTag(Object object)
    {
        
    }

    class MonthGridListener implements OnClickListener 
    {
        private int mPos;
        @SuppressWarnings("unused")
        private Context mContext;

        MonthGridListener(Context context, int pos) 
        {
            mContext = context;
            mPos = pos;
        }
        
        @Override
        public void onClick(View v) 
        {
            char[] monthMask = mPolicy.getMonthMask();
            if(mPos > monthMask.length)
            {
                return;
            }

            if(monthMask[mPos] == '1')
            {
                monthMask[mPos] = '0';
            }
            else
            {
                monthMask[mPos] = '1';
            }
            mPolicy.setMonthMask(monthMask);
            
            mMonthAdapter.notifyDataSetChanged();   
        }
    }
    
    class MonthDayGridListener implements OnClickListener 
    {
        private int mPos;
        @SuppressWarnings("unused")
        private Context mContext;

        MonthDayGridListener(Context context, int pos) 
        {
            mContext = context;
            mPos = pos;
        }
        
        @Override
        public void onClick(View v) 
        {
            char[] dayMask = mPolicy.getDayMask();
            if(mPos > dayMask.length)
            {
                return;
            }

            if(dayMask[mPos] == '1')
            {
                dayMask[mPos] = '0';
            }
            else
            {
                dayMask[mPos] = '1';
            }
            mPolicy.setDayMask(dayMask);
            
            mDayAdapter.notifyDataSetChanged();   
        }
    }

    @Override
    public void onClose()
    {
        
    }
}
